thin.imports('/public/home/css/global.project.css');

thin.imports('/public/configure/conf.project.js');
thin.imports('/public/configure/conf.msg.js');

thin.use(function() {
	proid = tool.GetQueryString('proid');

	// 路由
	checkLocation();

	$('#volist-template').on('click', '#btn-details', function(e) {
		var that = $(this);
		proid = that.attr('data-id');
		checkLocation('details');
	});

	$('#btn-msg-refuse').on('click', function() {
		$.showLoading();
		__getdata_msgrefuse['id'] = tool.GetQueryString('msgid');
		factory.request('_msg_refuse');
	});

	$('#btn-msg-chat').on('click', function() {
		$.showLoading();
		__getdata_msgchat['id'] = tool.GetQueryString('msgid');
		factory.request('_msg_chat');
	});

	$('#btn-msg-chat-s').on('click', function() {
		$.showLoading();
		__getdata_msgpull['project_id'] = proid;
		factory.request('_msg_pull');
	});

	$('#searchCancel').on('click', function() {
		delete __getdata_provolist['skey'];
		getVolist('res');
	});

	// 预览计划书
	$('#bind-pd-bp, #bind-pd-cp').on('click', function() {
		var uri = $(this).attr('data-uri');
		if(uri) {
			window.location.href = uri;
		} else {
			var str = '';
			if(this.id == 'bind-pd-bp') {
				str = '尚未上传商业计划书';
			} else if(this.id == 'bind-pd-cp') {
				str = '尚未进行项目估值';
			}
			$.toast(str, "text");
		}
	});
});

var __getdata_msgrefuse = {},
	__getdata_msgchat = {},
	__getdata_msgpull = {};

function __complete_msgrefuse(res) {
	$.hideLoading();
	if(res.status == 204) {
		window.location.href = '/mobile/service/member#msg-modal';
	}
}

function __complete_msgpull(res) {
	$.hideLoading();
	if(res.status == 201) {
		window.location.href = '/mobile/service/member#msg-modal';
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_msgchat(res) {
	$.hideLoading();
	if(res.status == 201) {
		$.modal({
			title: "系统提示",
			text: res.data.mobile,
			buttons: [{
					text: "拨打",
					onClick: function() {
						window.location.href = 'tel:' + res.data.mobile;
					}
				},
				{
					text: "确定",
					onClick: function() {
						window.location.href = '/mobile/service/member#msg-modal';
					}
				}
			]
		});
	}
}

var loading = false; //状态标记

var __getdata_provolist = {
	'show': 99
};

function getVolist(cmd) {
	$('#volist-loadmore').show();
	factory.request('_project_volist', (cmd || 'add'));
}

function __complete_provolist(res, unkn) {
	$('#volist-loadmore').hide();
	if(res.status == 200) {
		if (res.data.list.length > 0) {
			createVolist(res.data, unkn);
			loading = false;
			$('#project-volist').parent().removeClass('nothing-bg');
		} else {
			$('#project-volist').parent().addClass('nothing-bg');
		}
	} else {
	}
}

function createVolist(data, cmd) {
	var ele = '';
	for(var i in data.list) {
		var list = data.list[i];
		ele += '<a class="weui-cell" href="javascript:;" id="btn-details" data-id="' + list.id + '">';
		ele += '<div class="weui-cell__hd m-r-2x">';
		ele += '<img src="' + list.logo + '" class="pic-circle">';
		ele += '</div>';
		ele += '<div class="weui-cell__bd">';
		ele += '<p class="lh28x">' + list.title;
		ele += '<font class="pro-status">' + list.round + '</font>';
		ele += '</p>';
		ele += '<p class="c-888 fs12r ellipsis">' + list.words + '</p>';
		ele += '<p class="c-888 fs12r">' + list.city + '·' + list.industry + '</p>';
		ele += '</div>';
		ele += '</a>';
	}
	if(cmd == 'add') {
		$('#project-volist').append(ele);
	} else if(cmd == 'res') {
		$('#project-volist').html(ele);
	}
}

function __complete_proget(res) {
	$.hideLoading();
	if(res.status == 200) {
		proid_opt[res.data.id] = res.data;
		bindDetails(res.data);
	} else {

	}
}

function bindDetails(data) {
	// ----------------------------------------------------------------------------------------
	var ownerId = data.member_id, // 项目拥有者主键
		memberId = tool.__cookie.get('memberId'), // 当前人主键
		role = tool.__cookie.get('memberRole'), // 角色
		isCert = tool.__cookie.get('memberCert'), // 投资人是否通过认证
		isPull = tool.GetQueryString('isPull'), // 是否有投递关系
		isChat = tool.GetQueryString('ischat'); // 是否约谈
	if(role == 1) {
		// 创业者模式
		if(ownerId == memberId) {
			// 自己场景(完全展示)
			$('#box-pdf').css('display', 'block');
			$('#btn-msgbox').css('display', 'none');
		} else {
			// 一般人场景(不显示 商业计划书 和 评估报告)
			$('#box-pdf').css('display', 'none');
			$('#btn-msgbox').css('display', 'none');
		}
	} else {
		// 投资者模式
		if(isPull) {
			// 约谈者场景
			if(isChat == 1) {
				// 已约谈场景(完全展示 出现 『拒绝』按钮)
				$('#box-pdf').css('display', 'block');
				$('#btn-msgbox').find('.weui-flex__item').css('display', 'none');
				$('#btn-msgbox').css('display', 'block');
				$('#btn-msgbox').find('.bmx-1').css('display', 'block');
			} else {
				// 未约谈场景(完全展示 出现 『拒绝』『约谈』按钮)
				$('#box-pdf').css('display', 'none');
				$('#btn-msgbox').find('.weui-flex__item').css('display', 'none');
				$('#btn-msgbox').css('display', 'block');
				$('#btn-msgbox').find('.bmx-1').css('display', 'block');
				$('#btn-msgbox').find('.bmx-2').css('display', 'block');
			}
		} else {
			// 一般人场景
			if(isCert) {
				// 已认证投资人(完全展示 出现 『拉取项目』 按钮)
				$('#box-pdf').css('display', 'none');
				$('#btn-msgbox').find('.weui-flex__item').css('display', 'none');
				$('#btn-msgbox').css('display', 'block');
				$('#btn-msgbox').find('.bmx-3').css('display', 'block');
			} else {
				// 未认证投资人(不显示 商业计划书 和 评估报告)
				$('#box-pdf').css('display', 'none');
				$('#btn-msgbox').css('display', 'none');
			}
		}
	}
	// ----------------------------------------------------------------------------------------

	$('#bind-pd-logo').attr('src', data.logo);
	$('#bind-pd-title').text(data.title);
	$('#bind-pd-words').text(data.words);
	$('#bind-pd-introduction').text(data.introduction);
	$('#bind-pd-round').find('.value-text').text(data.round);
	$('#bind-pd-industry').find('.value-text').text(data.industry);
	$('#bind-pd-city').find('.value-text').text(data.city);
	$('#bind-pd-money').find('.value-text').text(data.money + '万元');

	if(data.company !== '' && data.team !== '') {
		$('#bind-pd-ct').css('display', 'block');
		if(data.company != '') {
			$('#bind-pd-company').css('display', 'flex').find('.value-text').text(data.company);
		}
		if(data.team !== '') {
			$('#bind-pd-team').css('display', 'flex').find('.value-text').text(data.team);
		}
	}

	if(data.members && data.members !== '') {
		var members = JSON.parse(data.members);
		if(members.length > 0) {
			var ele = '';
			for(var i in members) {
				var m = JSON.parse(members[i]);
				ele += '<a class="weui-cell" href="javascript:;">';
				ele += '<div class="weui-cell__bd">';
				ele += '<p>';
				var info = JSON.parse(m.info);
				ele += '<font class="p-r-1x">' + info.p_psn + '</font>';
				ele += m.name;
				ele += '</p>';
				ele += '</div>';
				ele += '</a>';
			}
			$('#members-title').css('display', 'block');
			$('#bind-pd-members').css('display', 'block').html(ele);
		}
	}

	if(data.bp != '') $('#bind-pd-bp').removeClass('c-dcdcdc').addClass('c-f47023').attr('data-uri', data.bp);
	$('#bind-pd-cp').attr('data-uri', data.cp_uri);
	if(data.atlas != '') {
		var atlas = JSON.parse(data.atlas);
		var ele = '';
		for(var i in atlas) {
			ele += '<img src="' + atlas[i] + '" />';
		}
		$('#bind-pb-atlas').html(ele);
	}
}

var __getdata_opts = {};

function __complete_opts(res, unkn) {
	var data_opt = res.data;
	data_opt.unshift({
		title: "全部",
		value: "",
	});
	if(res.status == 200) {
		var opts = {
			title: "请选择",
			items: data_opt,
			input: unkn[1],
			onClose: function(res) {
				if(res.data.titles != '') {
					var _node = $('#btn-pc-' + unkn[0]).find('.value-text');
					if(res.data.titles == '全部') {
						_node.html(_node.attr('data-title'));
					} else {
						_node.html(res.data.titles);
					}
					__getdata_provolist[unkn[0]] = res.data.values;
					getVolist('res');
				}
				//$('#ipt-pc-' + unkn[0]).val(res.data.titles);
			}
		};
		$('#btn-pc-' + unkn[0]).on('click', function(e) {
			var e = e || window.event;
			e.stopPropagation();
			e.preventDefault();
			$(this).select('open');
			return false;
		}).select(opts);
	} else {
		// nothing
	}
}

function searchs() {
	__getdata_provolist['skey'] = $('[name=skey]').val();
	getVolist('res');
	return false;
}

var proid = '';
var proid_opt = {},
	__getdata_proget = {
		'model': 1
	};
var jude_volist = false;

// 路由
function checkLocationCallback(hash) {
	if(hash == 'volist') {
		if(!jude_volist) {
			$.showLoading();
			getVolist();
			setTimeout(function() {
				$.hideLoading();
			}, 1200);
			/*$('#volist-template').infinite().on("infinite", function() {
				if(loading) return;
				loading = true;
				getVolist();
			});*/
			thin.imports('/public/configure/conf.opts.js', function() {
				__getdata_opts.name = 'round';
				factory.request('_common_opts', ['round', '']);
				__getdata_opts.name = 'industry';
				factory.request('_common_opts', ['industry', '']);
				__getdata_opts.name = 'city';
				factory.request('_common_opts', ['city', '']);
				setTimeout(function() {
					$.hideLoading();
				}, 1200);
			});
			jude_volist = true;
		}
	}

	if(hash == 'details') {
		if(!proid_opt[proid]) {
			$.showLoading();
			__getdata_proget['project_id'] = proid;
			factory.request('_project_get');
		} else {
			bindDetails(proid_opt[proid]);
		}
	}
}